	player= 1;// GET player from outside
// BOARD:
	current= false;
	first = true;
	moved_two = false;
	piece= false;

	var board = document.createElementNS("http://www.w3.org/2000/svg", "svg");
	board.setAttribute("style", "background: red");

	board.setAttribute("height", "470");
    board.setAttribute("width", "470");

	document.getElementById("boardplace").appendChild(board);
		for (var i=0;i<10;i++)
		{
			for (var j=0;j<10;j++)
			{
				var shape = document.createElementNS("http://www.w3.org/2000/svg", "circle");
				shape.setAttribute("id", i +"-"+ j);
				shape.setAttribute("onmousedown","mouse_down(evt)");
				shape.setAttribute("onmouseup","mouse_up(evt)");
				shape.setAttribute("cx", 30 + i*45);
				shape.setAttribute("cy", 30 + j*45);
				shape.setAttribute("r",  20);
				shape.setAttribute("fill", "white");
				shape.setAttribute("class", "piece");
				if (i+j < 3)
				{
					shape.setAttribute("class", "firstP");
				}else if (i+j>  15 )
				{
					shape.setAttribute("class", "secondP");
				}
				
				board.appendChild(shape);
			}
		}
	
	
	function mouse_down(evt)
	{
		current = evt.target["id"];
	}
	
	function mouse_up(evt)
	{
		
			if (! valid_move("blue",current, evt.target["id"])) 
			{
				return;
			}
			first=false;
			if (! moved_two)
			{
				document.getElementById("turn_status").innerHTML= "<b>turn ended</b>";
			}
			
			var cr_obj = document.getElementById(current);
			
			var tmp = cr_obj.getAttribute("class");
			cr_obj.setAttribute("class", "piece");
			evt.target.setAttribute("class", tmp); //TO update based on current pla
			document.getElementById("moves").value += current + "->"+ evt.target["id"] + ";";
		
		current=false;
	}
	
	function submit_move()
	{
		var xmlhttp;
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
	    {// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
				alert(xmlhttp.responseText);
				//document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
	    }
	  }
		xmlhttp.open("POST","GamePage",true);
		xmlhttp.send("user=" + getQueryVariable("user_id") +
				"&moves=" + document.getElementById("moves").value);
//		xmlhttp.send("user=" + getQueryVariable("user") +
//				"&password=" + getQueryVariable("password") +
//				"&moves=" + 	document.getElementById("moves").value);
	}
	function getQueryVariable(variable) {
	   //alert(variable);
	   var query = window.location.search.substring(1);
	   alert(query);
	   return;
	   var vars = query.split("&");
	   for (var i=0;i<=vars.length;i++) {
	      var pair = vars[i].split("=");
	      if (pair[0] == variable) {
	    	  alert(variable);
	    	  return pair[1];
		  }
	   }
	   return "";
	}

	function valid_move(turn, from, to)
	{
		if (piece && piece != from)
		{
			return;
		}
		//check if have more moves:
		if (! first && ! moved_two){
			return false;
		}
		moved_two=false;
		//in place:
		if (! current || to == current) return false;
		//can't move to not empty spot:
		if  (document.getElementById(from).getAttribute("class") == "piece") return false;
		if  (document.getElementById(to).getAttribute("class") != "piece") return false;

		var a = from.split("-");
		var b = to.split("-");
		var x1 = parseInt(a[0]);
		var y1 = parseInt(a[1]);
		
		var x2 = parseInt(b[0]);
		var y2 = parseInt(b[1]);

		//cant move two if don't have anything in the middle;
		if ( ((Math.abs(x1 - x2) == 2) && (Math.abs(y1 - y2) == 0)) ||
		     ((Math.abs(x1 - x2) == 0) && (Math.abs(y1 - y2) == 2)) ||
		     ((Math.abs(x1 - x2) == 2) && (Math.abs(y1 - y2) == 2)) )
		{
			var p_id =  (x1+x2)/2 + "-" + (y1+y2)/2;
			if (document.getElementById(p_id).getAttribute("class") != "piece") 
			{
				piece= to;
				moved_two=true;	
				return true;
			}
			return false;
		}
		if (piece)
		{
		//can move only two if did jump
			return false
		}
		
		//else can move only one:
		if ((Math.abs(x1 - x2) == 1) 
			|| (Math.abs(y1 - y2) == 1))
			{
				piece = to;
				return true;
			}
		//illegal move:	
		return false;
		
	}
	
	function game_rules()
	{
		alert(
		"this game designed for two players.\n\n"+
		"In each turn the player can move one of his pieces to an empty spot,"+
		"one up|down|diagonaly\n"+
		"or two up|down|diagonaly if there is a piece in the middle."		
		);
	}